package com.suxii.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.ColorSpace;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.suxii.netty_demo.R;
import com.suxii.pojo.MessageMsg;
import com.suxii.utils.Global;

import java.util.List;

public class LeftMessageAdapter extends RecyclerView.Adapter<LeftMessageAdapter.MessageViewHolder> {

    private List<MessageMsg> data;
    public Context context;



    public LeftMessageAdapter() {
    }

    public LeftMessageAdapter(List<MessageMsg> data, Context context) {
        this.data = data;
        this.context = context;
    }


    @NonNull
    @Override
    public LeftMessageAdapter.MessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_left_message, parent, false);
        return new MessageViewHolder(view);
    }


    //记录上一个item的holder
    private LeftMessageAdapter.MessageViewHolder firstHolder=null;

    @Override
    public void onBindViewHolder(@NonNull LeftMessageAdapter.MessageViewHolder holder, int position) {

        //第一次加载初始化
        if (data.get(position).getName().equals(String.valueOf(Global.uuid))){
            holder.itemView.setBackgroundResource(R.drawable.icon_list);
            firstHolder=holder;
        }
        holder.name.setText(data.get(position).getName());
        holder.itemView.setOnClickListener(view -> {
            //4

            if (firstHolder!=null){
                firstHolder.itemView.setBackgroundResource(R.color.bg_zi);
            }
            itemListener.onItemClick(data.get(position));
            holder.itemView.setBackgroundResource(R.drawable.icon_list);
            firstHolder=holder;
        });

    }

    @Override
    public int getItemCount() {
        return data.size();
    }


    //1.自定义接口
    public interface ItemListener{
        void onItemClick(MessageMsg pos);
    }
    //2.声明自定义接口
    private ItemListener itemListener;
    //3.定义一个方法
    public void setOnItemClickListener(ItemListener itemListener){
        this.itemListener=itemListener;
    }


    class MessageViewHolder extends RecyclerView.ViewHolder {

        private TextView name;

        public MessageViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.item_left_message_name);

        }
    }

}
